#include "obscuredplaneactor.h"
#include "stringproperty.h"
#include "datamanager.h"
#include "resourceloader.h"

namespace VrDataCore
{
	ObscuredPlaneActor::ObscuredPlaneActor():_image("")
	{
		setClassName(CLASS_OBSCUREDPLANE);
	}

	ObscuredPlaneActor::~ObscuredPlaneActor()
	{}

	void ObscuredPlaneActor::buildPropertyMap()
	{
		addProperty(new StringProperty<DrawableActor>(XML_ELEMENT_DATATYPE_STRING,
			"Parent ID","",this,&DrawableActor::setParentID,
			&DrawableActor::getParentID,"",""));
		addProperty(new StringProperty<ObscuredPlaneActor>(XML_ELEMENT_DATATYPE_STRING,
			"Vertex List","",this,&ObscuredPlaneActor::setVertexList,
			&ObscuredPlaneActor::getVertexList,"",""));
		addProperty(new StringProperty<ObscuredPlaneActor>(XML_ELEMENT_DATATYPE_STRING,
			"Texture","",this,&ObscuredPlaneActor::setPlaneImage,
			&ObscuredPlaneActor::getPlaneImage,"",""));
	}
	
	void ObscuredPlaneActor::createDrawable()
	{
		_drawable = new CoreBase::Transformable(this);
		initialNode();
	}
	
	osg::ref_ptr<CoreBase::ActorBase> ObscuredPlaneActor::clone()
	{
		osg::ref_ptr<CoreBase::ActorBase> clone = DrawableActor::clone();
		return clone;
	}

	void ObscuredPlaneActor::setVertexVector(std::vector<osg::Vec3> vector)
	{
		_vertexVector = vector;
		getStateSet();
		if(_vertexVector.size() == 3)
			_plane->reset(_vertexVector.at(0),_vertexVector.at(1),_vertexVector.at(2),osg::Vec2(0.3f,0.3f),
			osg::Vec2(0.8f,0.3f),osg::Vec2(0.8f,0.8f),_ss);
		else if(_vertexVector.size() == 4)
		{
			_plane->reset(_vertexVector.at(0),_vertexVector.at(1),_vertexVector.at(2),_vertexVector.at(3),
				osg::Vec2(0.0f,0.0f),osg::Vec2(1.0f,0.0f),osg::Vec2(1.0f,1.0f),osg::Vec2(0.0f,1.0f),_ss);
		}
	}

	std::vector<osg::Vec3> ObscuredPlaneActor::getVertexVector()
	{
		return _vertexVector;
	}

	void ObscuredPlaneActor::setVertexList(QString vertex)
	{
		_vertexList = vertex;
	}

	QString ObscuredPlaneActor::getVertexList()
	{
		_vertexList= "";
		for(int i=0;i<_vertexVector.size();++i)
		{
			QString everypoint = QString::number(_vertexVector.at(i).x())+" "+QString::number(_vertexVector.at(i).y())+" "
				+ QString::number(_vertexVector.at(i).z());
			_vertexList = _vertexList + "," + everypoint;
		}
		return _vertexList;
	}

	void ObscuredPlaneActor::setPlaneImage(QString image)
	{
		_image = image;
		getStateSet();
		if(_vertexVector.size() == 3)
			_plane->reset(_vertexVector.at(0),_vertexVector.at(1),_vertexVector.at(2),osg::Vec2(0.3f,0.3f),
			osg::Vec2(0.8f,0.3f),osg::Vec2(0.8f,0.8f),_ss);
		else if(_vertexVector.size() == 4)
			_plane->reset(_vertexVector.at(0),_vertexVector.at(1),_vertexVector.at(2),_vertexVector.at(3),
			osg::Vec2(0.0f,0.0f),osg::Vec2(1.0f,0.0f),osg::Vec2(1.0f,1.0f),osg::Vec2(0.0f,1.0f),_ss);
	}

	QString ObscuredPlaneActor::getPlaneImage()
	{
		return _image;
	}

	void ObscuredPlaneActor::getVertex()
	{
		_vertexVector.clear();
		QStringList vertexlist = _vertexList.split(",",QString::SkipEmptyParts);
		for(int i=0;i<vertexlist.size();++i)
		{
			QStringList everyvertex = vertexlist.at(i).split(" ",QString::SkipEmptyParts);
			osg::Vec3 point;
			if(everyvertex.size() == 3)
			{
				point.x() = everyvertex.at(0).toFloat();
				point.y() = everyvertex.at(1).toFloat();
				point.z() = everyvertex.at(2).toFloat();
				_vertexVector.push_back(point);
			}
		}
	}

	void ObscuredPlaneActor::initialNode()
	{
		_plane = new CoreBase::PlaneShape();
		this->getDrawable()->addChild(_plane);
		//setTraverseMask(NODE_MASK_NOSELECT);
	}

	void ObscuredPlaneActor::onMapLoadEnd(CoreBase::Map* map)
	{
		getVertex();
		getStateSet();
		if(_vertexVector.size() == 3)
			_plane->reset(_vertexVector.at(0),_vertexVector.at(1),_vertexVector.at(2),osg::Vec2(0.3f,0.3f),
			osg::Vec2(0.8f,0.3f),osg::Vec2(0.8f,0.8f),_ss);
		else if(_vertexVector.size() == 4)
			_plane->reset(_vertexVector.at(0),_vertexVector.at(1),_vertexVector.at(2),_vertexVector.at(3),
			osg::Vec2(0.0f,0.0f),osg::Vec2(1.0f,0.0f),osg::Vec2(1.0f,1.0f),osg::Vec2(0.0f,1.0f),_ss);
	}

	void ObscuredPlaneActor::getStateSet()
	{
		if(_image!="")
		{
			if(!_ss.valid())
				_ss= VrDataCore::DataManager::GetInstance().getPublicStateSetByDrawableName(_image);
			osg::ref_ptr<osg::Image> imagepath = CoreBase::ResourceLoader::GetInstance().getImageByFileName(_image);
			if(imagepath.valid())
			{
				osg::Texture2D* tex2d = new osg::Texture2D;
				tex2d->setImage(imagepath);
				_ss->setTextureAttributeAndModes(0,tex2d,osg::StateAttribute::ON);
				_ss->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
			}
		}
	}

	void ObscuredPlaneActor::showPlaneLine(bool show)
	{
		if(_plane.valid())
			_plane->showPlaneLine(show);
	}

	void ObscuredPlaneActor::updateLine(std::vector<osg::Vec3> pointvector)
	{
		if(_plane.valid())
			_plane->updateLine(pointvector);
	}
}